package com.youmingxing.activity.selfInfo;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.bean.User;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.network.SelfInfoApi;
import com.youmingxing.utils.Tools;
import com.youmingxing.view.TextLengthWatcher;

import org.json.JSONObject;

import static com.youmingxing.bean.User.Info;

/**
 * @author evilyin(ChenZhixi)
 * @since 2015-10-13
 */

public class EditTextActivity extends BaseActivity {

    private EditText editText;
    private int option;
    private String text;//默认显示的文字

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_edit_text);
        setTopbarName(R.string.edit);

        Bundle bundle = getIntent().getExtras();
        option = bundle.getInt("option");

        text = bundle.getString("text","");

        editText = (EditText) findViewById(R.id.edit_text);
        editText.setText(text);

        if (option == -1) {
            //从完善资料那边过来的,设置的肯定是昵称
            setLeftAndrightTv(true, true, false, R.string.finish, null, listenerCompleteInfo, null);
            editText.addTextChangedListener(new TextLengthWatcher(20, editText));
        }else{
            //编辑资料
            switch (User.Info.values()[option]) {
                case nickname:
                    editText.addTextChangedListener(new TextLengthWatcher(20,editText));
                    break;
                case motto:
                    editText.addTextChangedListener(new TextLengthWatcher(200,editText));
                    break;
                case career_or_college:
                    editText.addTextChangedListener(new TextLengthWatcher(40,editText));
                    break;
                case link_email:
                    editText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
                    break;
                case link_mobile:
                    editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                    editText.addTextChangedListener(new TextLengthWatcher(11, editText));
                    break;
            }
            setLeftAndrightTv(true, true, false, R.string.finish, null, listener, null);
        }
    }

    View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = editText.getText().toString();
            String thisOption = Info.values()[option].toString();
            if (text.isEmpty()) {
                showToast("您还没有输入内容哦");
                return;
            }
            if (option == Info.link_mobile.ordinal() && !Tools.isMobileNum(text)) {
                showToast("您输入的手机号格式有误,请重新输入");
                return;
            }
            if (option == Info.link_email.ordinal() && !Tools.isEmail(text)) {
                showToast("您输入的邮箱格式有误,请重新输入");
                return;
            }
            SelfInfoApi.modifyUserProfile(thisOption, text, new BaseJsonHandler(EditTextActivity
                    .this) {
                @Override
                public void onSuccessWithData(JSONObject response) {
                    if (response.optJSONObject("data").optInt("result") == 1) {
                        showToast("修改成功");
                    }
                }
            });
            Intent intent = new Intent();
            intent.putExtra("option", Info.valueOf(thisOption).ordinal());
            intent.putExtra("value", text);
            setResult(RESULT_OK, intent);
            finish();
        }
    };

    View.OnClickListener listenerCompleteInfo=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String text = editText.getText().toString();
            if (text.isEmpty()) {
                showToast("您还没有输入内容哦");
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("value", text);
            setResult(RESULT_OK, intent);
            finish();
        }
    };
}
